// 屏幕适配 mixin 函数

// * 默认缩放值
const scale = {
  width: '1',
  height: '1',
}

// * 设计稿尺寸（px）
const baseWidth = 1920
const baseHeight = 1080

export default {
  data() {
    return {
      // * 定时函数
      drawTiming: null
    }
  },
  mounted () {
    this.calcRate(1)
    setTimeout(() => {
      this.calcRate(1)
    }, 200)
    window.addEventListener('resize', this.resize)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.resize)
  },
  methods: {
    calcRate (num) {
      const appRef = this.$refs["appRef"]
      if (!appRef) return
      if (appRef) {
        scale.width = (document.documentElement.clientWidth / baseWidth).toFixed(5)
        scale.height = (document.documentElement.clientHeight / baseHeight).toFixed(5)
        appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
        console.log(num, document.documentElement.clientWidth, document.documentElement.clientHeight)
      }
    },
    resize () {
      this.drawTiming && clearTimeout(this.drawTiming)
      this.calcRate()
    }
  },
}
